package com.vanzo.some.appinfo;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.vanzo.some.R;

import java.util.List;

/**
 * Created by Administrator on 2017/4/5.
 */

public class PackageAdapter extends RecyclerView.Adapter<PackageAdapter.PackageItem> {
    private List<AppInfo> infos;
    private Context context;
    private PackageManager manager;

    public PackageAdapter(List<AppInfo> infos, Context context) {
        this.infos = infos;
        this.context = context;
        manager = context.getPackageManager();
    }

    @Override
    public PackageItem onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PackageItem(LayoutInflater.from(parent.getContext()).inflate(R.layout.package_item, parent, false));
    }

    @Override
    public void onBindViewHolder(PackageItem holder, int position) {
        holder.setInfo(infos.get(position));
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            PackageItem item = (PackageItem) msg.obj;
            android.content.pm.PackageInfo info1 = null;

            try {
                info1 = manager.getPackageInfo(item.packageName.getText().toString(), 0);
                item.version.setText(info1.versionName);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    public int getItemCount() {
        return infos.size();
    }

    class PackageItem extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView label;
        TextView version;
        TextView packageName;
        AppInfo info;

        public PackageItem(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.item_icon);
            label = (TextView) itemView.findViewById(R.id.item_label);
            version = (TextView) itemView.findViewById(R.id.item_version);
            packageName = (TextView) itemView.findViewById(R.id.item_package_name);
//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent();
//                    intent.setClass(context, InfoActivity.class);
//                    intent.putExtra("info", info);
//                    context.startActivity(intent);
//                }
//            });
        }

        public void setInfo(AppInfo info) {
            icon.setImageDrawable(info.getIcon());
            label.setText(info.getLabel());
            version.setText(info.getVersion());
            packageName.setText(info.getPackageName());
            Message message = new Message();
            message.obj = this;
            handler.sendMessage(message);
            this.info = info;
        }
    }
}
